package cn.software.controller;

import cn.software.entity.param.*;
import cn.software.entity.vo.*;
import cn.software.service.TestService;
import cn.software.util.Result;
import cn.software.util.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试接口
 */
@RestController
@RequestMapping("/test")
@Api(tags = "考试接口")
public class TestController {

    @Autowired
    private TestService testService;

    @PostMapping("/addTest")
    @ApiOperation("增加考试")
    public Result addTest(@RequestBody TestParam testParam){
        int isSuccess = testService.addTest(testParam);
        if (isSuccess == -1){
            return Result.success(null);
        }else if(isSuccess == -2){
            return Result.fail("开始时间不能小于当前时间");
        }else{
            return Result.testAddFail(testService.getTestById(isSuccess));
        }
    }

    @GetMapping("/getTestInfById")
    @ApiOperation("根据id查询考试数据")
    public Result getTestInfById(@RequestParam Integer id){
        List<StuTestVo> listStuTestVo = testService.getTestInfById(id);
        return Result.success(listStuTestVo);
    }

    @GetMapping("/getAllTest")
    @ApiOperation("查询所有考试数据")
    public Result getAllTest(){
        List<TestVo> listTestVo = testService.getAllTest();
        return Result.success(listTestVo);
    }

    @PostMapping("/deleteTestById")
    @ApiOperation("根据id删除考试")
    public Result deleteTestById(@RequestParam Integer id){
        int isSuccess = testService.deleteTestById(id);
        if(isSuccess > 0){
            return Result.success(null);
        }else if(isSuccess == -3){
            return Result.fail("考试正在进行");
        }
        else {
            return Result.fail("考试已经结束");
        }
    }

    @PostMapping("/updateTest")
    @ApiOperation("修改考试信息")
    public Result updateTest(@RequestBody TestParam testParam){
        int isSuccess = testService.updateTest(testParam);
        if (isSuccess == -1){
            return Result.success(null);
        }else{
            return Result.testAddFail(testService.getTestById(isSuccess));
        }
    }

    @PostMapping("/takeTest")
    @ApiOperation("参加考试")
    public Result takeTest(@RequestBody TakeTestParam takeTestParam){
        int takeTestTime = testService.isConflictTestTime(takeTestParam);
        if(takeTestTime == 1){
            return Result.fail("考试未开始");
        }else if(takeTestTime == 2){
            return Result.fail("考试已结束");
        }else{
            return Result.success(testService.takeTest(takeTestParam));
        }
    }

    @PostMapping("/submitTestPaper")
    @ApiOperation("提交试卷")
    public Result submitTestPaper(@RequestBody StuTestRecordParam stuTestRecordParam){
        boolean isSuccess = testService.submitTestPaper(stuTestRecordParam);
        if (isSuccess){
            return Result.success(null);
        }else{
            return Result.fail("提交失败");
        }
    }

    @GetMapping("/getSAQ")
    @ApiOperation("查询简答题（老师批改试卷）")
    public Result getSAQ(@RequestParam Integer stuTestId){
        List<TopicAnswerVo> listTopicAnswerVo = testService.getSAQ(stuTestId);
        return Result.success(listTopicAnswerVo);
    }

    @PostMapping("/teacherCorrectsPaper")
    @ApiOperation("老师手动批改试卷")
    public Result teacherCorrectsPaper(@RequestBody TeacherCorrectPaperParam teacherCorrectPaperParam){
        boolean isSuccess = testService.teacherCorrectsPaper(teacherCorrectPaperParam);
        if (isSuccess){
            return Result.success(null);
        }else{
            return Result.fail("提交失败");
        }
    }

    @GetMapping("/getTree")
    public Result getTree(@RequestParam Integer teacherId){
        TreeVo treeVo = testService.getTree(teacherId);
        return Result.success(treeVo);
    }

    @GetMapping("/getTime")
    public Result getTime(@RequestParam Integer teacherId){
        TimeVo timeVo = testService.getTime(teacherId);
        return Result.success(timeVo);
    }


}
